package com.ansion.commonchat;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.multidex.MultiDexApplication;

import com.ansion.utils.ResourceUtils;
import com.ansion.utils.UiUtils;
import com.ansion.utils.dark.setting.DarkBroadcast;
import com.ansion.utils.dark.setting.DarkSetting;
import com.ansion.utils.thread.ThreadPool;

public class BaseApplication extends MultiDexApplication {

    private long mAppStartupInitStartTime = 0L;

    private static long mAppStartupInitTime = 0L;

    @Override
    protected void attachBaseContext(Context base) {
        //startTime
        appStartupTrace();
        ResourceUtils.initApplication(this);
        super.attachBaseContext(base);
        UiUtils.checkDisplaySize(this, null);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        applyAppDarkMode();
        //注册监听每个activity的生命周期,便于堆栈式管理
        this.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {

            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
//                AppManager.getAppManager().addActivity(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {
            }

            @Override
            public void onActivityResumed(Activity activity) {
                ThreadPool.triggerTaskSchedulerCheck();
            }

            @Override
            public void onActivityPaused(Activity activity) {
            }

            @Override
            public void onActivityStopped(Activity activity) {
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
//                AppManager.getAppManager().removeActivity(activity);
            }
        });
        //endTime
        appStartupTrace();
    }

    public static Context getContext() {
        return ResourceUtils.getContext().getApplicationContext();
    }

    public static Application getApplication() {
        return ResourceUtils.getApplication();
    }

    private void applyAppDarkMode() {
        DarkSetting.getInstance().applyCurrentTheme();
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        int currentNightMode = newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK;
        if (DarkSetting.getInstance().onConfigurationChanged(currentNightMode)) {
            newConfig.uiMode = currentNightMode;
            Intent intent = new Intent(DarkBroadcast.DARK_ACTION);
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
        }
        super.onConfigurationChanged(newConfig);
        UiUtils.checkDisplaySize(this, newConfig);
    }

    //对外暴露
    private void appStartupTrace() {
        if (mAppStartupInitStartTime != 0L) {
            mAppStartupInitTime = System.currentTimeMillis() - mAppStartupInitStartTime;
        }
        mAppStartupInitStartTime = System.currentTimeMillis();
    }

    //对外暴露
    public static long getAppStartupInitTime() {
        return mAppStartupInitTime;
    }
}
